<template>
    <orh-table pages :pageDate="pageDate" @on-page-change="changepage"
               search @on-search='searchblurry' reload @on-reload="refresh">
        <Select slot="option" v-model="nowxqid" @on-change="xqchange" style="width:200px">
            <Option v-for="item in xqlist" :value="item.semester_id" :key="item.semester_id">{{ item.semester_name.zh?
                item.semester_name.zh: item.semester_name.mn }}
            </Option>
        </Select>
        <Table slot="table" border :columns="columns" :data="datas"
               ref="currentRowTable" :loading="loading">
        </Table>
        <orh-sidebar slot="others" v-model="isShow" title="公文详情" scale>
            <dr-detail :id="id" @reload="load" @close="isShow=false" :userId='userId'></dr-detail>
        </orh-sidebar>
    </orh-table>
</template>

<script>

    export default {
        layout: "menu",
        name: 'documents-docreceive',
        data() {
            return {
                isShow: false,
                pageDate: {},
                id: '',
                userId: '',
                datas: [],
                docutypes: [],
                loading: false,
                modstau: false,
                columns: [
                    {
                        title: '标题',
                        render: (h, params) => {
                            return h('span', params.row.title.zh ? params.row.title.zh : params.row.title.mn)
                        }
                    },
                    {
                        title: '来文机关',
                        render: (h, params) => {
                            return h('span', params.row.civil_service.zh ? params.row.civil_service.zh : params.row.civil_service.mn)
                        }
                    },
                    {
                        title: '来文字号',
                        render: (h, params) => {
                            return h('span', params.row.letter_number.zh ? params.row.letter_number.zh : params.row.letter_number.mn)
                        }
                    },
                    {
                        title: '收文时间',
                        key: 'receipt_time',
                    },
                    {
                        title: '等级文号',
                        key: 'document_code',
                    },
                    {
                        title: '等级',
                        render: (h, params) => {
                            return h('span', params.row.grade.zh ? params.row.grade.zh : params.row.grade.mn)
                        }
                    },
                    {
                        title: '类型',
                        render: (h, params) => {
                            return h('span', params.row.dictionary.dictionary_name.zh || params.row.dictionary.dictionary_name.mn)
                        }
                    },
                    {
                        title: '拟办意见',
                        render: (h, params) => {
                            return h('span', params.row.options.zh ? params.row.options.zh : params.row.options.mn)
                        }
                    },
                    {
                        title: '查看状态',
                        render(h, params) {
                            let str = ''
                            let color = ''
                            switch (params.row.is_look) {
                                case 0:
                                    str = '未查看'
                                    color = '#007d7f'
                                    break;
                                case 1:
                                    str = '已查看'
                                    color = '#19be6b'
                                    break;
                            }
                            return h('span', {style: {color}}, str)
                        }
                    },
                    {
                        title: '操作',
                        key: 'operation',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.lookDetail(params.row)
                                    }
                                }
                            }, '查看')
                        }
                    }
                ],
                checkUrl: 'api/document/v1/document/my_look/',
                url: 'api/document/v1/document/my_document',
                pageUrl: 'api/document/v1/document/my_document?page=',
                nowxqid: '',
                xqlist: []
            }
        },
        methods: {
            load(id = null) {
                let url = 'api/document/v1/document/my_document';
                if (id) url += '?semester_id=' + id
                this.loading = true
                this.$http.get(url).then(val => {
                    const page = {
                        current_page: val.current_page,
                        current_page: val.current_page,
                        first_page_url: val.first_page_url,
                        from: val.from,
                        last_page: val.last_page,
                        last_page_url: val.last_page_url,
                        next_page_url: val.next_page_url,
                        path: val.path,
                        per_page: val.per_page,
                        prev_page_url: val.prev_page_url,
                        to: val.to,
                        total: val.total
                    }
                    this.pageDate = page
                    this.datas = val.data.data
                    this.loading = false
                }).catch(err => {
                    console.log(err)
                    this.loading = false
                })
            },
            //搜索
            searchblurry(val) {
                this.loading = true
                let value = 1
                this.$http.get('api/document/v1/document/my_document?searchvalue=' + val + '&page=' + value).then(val => {
                    if (val.code == 200) {
                        this.pageSize = val.data.per_page
                        this.dataCount = val.data.total
                        this.current = val.data.current_page
                        this.datas = val.data.data
                    }
                    this.loading = false
                }, err => console.log(err))
            },
            //刷新
            refresh() {
                this.changepage(this.pageDate.current_page)
            },
            lookDetail(item) {
                this.isShow = true
                this.id = item.document_id
                this.userId = item.user
                if (item.is_look == 0) {
                    this.$http.put(this.checkUrl + item.document_id).then(val => {
                        this.load()
                    }).catch(
                        err => console.log(err)
                    )
                }
            },
            changepage(index) {
                this.loading = true
                this.$http.get(this.pageUrl + index).then(val => {
                    this.pageSize = val.data.per_page
                    this.dataCount = val.data.total
                    this.current = val.data.current_page
                    this.datas = val.data
                    this.loading = false
                }, err => console.log(err))
            },
            getnowxq() {
                this.$http.get('api/semester/v1/semester/now_semester').then(res => {
                    this.nowxqid = res.data[1].semester_id
                }).catch(err => {
                    console.log('err', err)
                })
            },
            getxqlist() {
                this.$http.get('api/semester/v1/semester/semesterlists').then(res => {
                    console.log('xqlist', res)
                    this.xqlist = res.data
                }).catch(err => {
                    console.log('err', err)
                })
            },
            xqchange(val) {
                this.load(val)
            }
        },
        components: {
            DrDetail: re => require(['./DrDtail.vue'], re)
        },
        created() {
            this.load()
            this.getnowxq()
            this.getxqlist()
        }
    }
</script>
